﻿using System;
using System.Collections.Generic;
using FlatRedBall;
using FlatRedBall.Input;
using FlatRedBall.Graphics;
using FlatRedBall.Gui;
using FlatRedBall.Math.Geometry;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using BulletHellBoxes.Utilities;
using BulletHellBoxes.Entities.Menu;

namespace BulletHellBoxes.Screens
{
    public class MenuScreen : Screen
    {
        private Sprite background;
        private MenuButton exitButton;
        private MenuButton newGameButton;
        private MenuButton _controlsButton;
        #region Methods

        #region Constructor and Initialize

        public MenuScreen()
            : base("MenuScreen")
        {
            // Don't put initialization code here, do it in
            // the Initialize method below
            //   |   |   |   |   |   |   |   |   |   |   |
            //   |   |   |   |   |   |   |   |   |   |   |
            //   V   V   V   V   V   V   V   V   V   V   V
            //Initialize(true);
        }

        public override void Initialize(bool addToManagers)
        {
            if (addToManagers)
            {
                AddToManagers();
            }
        }

        public override void AddToManagers()
        {
            // Set the screen up here instead of in the Constructor to avoid
            // exceptions occurring during the constructor.
            FlatRedBallServices.IsWindowsCursorVisible = true;

            // Initialize the sprites
            // Initialize background sprite
            background = SpriteManager.AddSprite(@"Content/Menu/MenuBG");
            background.PixelScale();
            this.mSprites.Add(background);

            exitButton = new MenuButton("Global", @"Content/Menu/ExitButton", "", 'e', 0, -12);
            exitButton.collision.Visible = false;
            newGameButton = new MenuButton("Global", @"Content/Menu/PlayButton", "", 'n', 0, -4);
            newGameButton.collision.Visible = false;
            _controlsButton = new MenuButton("Global", @"Content/Menu/ControlsButton", "", 'n', 0, -8);
            _controlsButton.collision.Visible = false;

        }
        #endregion

        #region Public Methods

        public override void Activity(bool firstTimeCalled)
        {
            checkCollisions();
        }

        public override void Destroy()
        {
            base.Destroy();
            exitButton.Destroy();
            newGameButton.Destroy();
            _controlsButton.Destroy();
            SpriteManager.RemoveSprite(background);

        }

        private void checkCollisions()
        {
            // Mouse collision check
            // Is Mouse on any button?
            if (InputManager.Mouse.IsOn(exitButton.collision))
            {
                exitButton.Hover();
                if (InputManager.Mouse.ButtonPushed(FlatRedBall.Input.Mouse.MouseButtons.LeftButton))
                {
                    FlatRedBallServices.Game.Exit();
                }
            }
            else
                exitButton.Idle();

            if (InputManager.Mouse.IsOn(newGameButton.collision))
            {
                newGameButton.Hover();
                // Is left button pressed?
                if (InputManager.Mouse.ButtonPushed(FlatRedBall.Input.Mouse.MouseButtons.LeftButton))
                {
                    Destroy();
                    MoveToScreen(typeof(GameScreen).FullName);

                }
            }
            else
                newGameButton.Idle();

            if (InputManager.Mouse.IsOn(_controlsButton.collision))
            {
                _controlsButton.Hover();
                // Is left button pressed?
                if (InputManager.Mouse.ButtonPushed(FlatRedBall.Input.Mouse.MouseButtons.LeftButton))
                {
                    Destroy();
                    MoveToScreen(typeof(ControlScreen).FullName);

                }
            }
            else
                _controlsButton.Idle();
        }
        #endregion

        #endregion
    }
}
